﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Mono.Data.Sqlite;//引入数据库

public class PlayerDataManager : MonoBehaviour {

    public InputField inputUserName;
    string userName;

    //int ToolsMoney;
    //string addProp;


    bool passRound = false;
    int grade = 0;
    int rank = 0;
    string prop = null;
    int money = 0;

    //储存数据对象
    private SqliteConnection sqlConnection = null;

    void Start() {
        //链接到魔方数据库
        SqliteHelper.GetInstance().ConnectionToSqlite("magicSquare");
        userName = inputUserName.text;
    }

    void Update() {

    }

    #region 判断用户名是否存在
    ///// <summary>
    ///// 判断用户名是否存在
    ///// </summary>
    ///// <param name="userName"></param>
    //public void CheckUserName(string userName)
    //{
    //    string[] userNames = SqliteHelper.GetInstance().ReadData(SqliteHelper.GetInstance().SelectALL("MagicSquare"), "userName");
    //    foreach (string username in userNames)
    //    {
    //        if (userName == username)
    //        {
    //            //如果用户名存在,可直接获取用户信息,用户名即为输入的名称
    //            userName = username;


    //        }
    //        else
    //        {
    //            //如果用户名不存在,创建用户信息
    //            //SqliteHelper.GetInstance().IDU("insert into magicSquare values ('{0}',{1},{2},{3},'{4}',{5})", username,passRound,grade,rank,prop,money);
    //            SqliteHelper.GetInstance().IDU("insert into MagicSquare values ('" + username + "'," + passRound + "," + grade + "," + rank + ",'" + prop + "'," + money + ")");
    //        }
    //    }
    //}
    #endregion
    
    
    /// <summary>
    /// 存入用户名
    /// </summary>
    /// <param name="name">输入的用户名</param>
    /// <returns></returns>
    public void SetUserName(string name)
    {
        SqliteHelper.GetInstance().IDU("insert into MagicSquare values ('" + name + "'," + passRound + "," + grade + "," + rank + ",'" + prop + "'," + money + ")");
    }

    /// <summary>
    /// 获取用户名
    /// </summary>
    /// <returns>返回用户名</returns>
    public string GetUserName()
    {
        object obj = SqliteHelper.GetInstance().SelectRow("userName", "MagicSquare", "userName", userName);
        string data = obj + "";
        string username = data;
        return username;
    }

    /// <summary>
    /// 更新现有金币(和剩余步数有关)
    /// </summary>
    public void SetHasMoney()
    {
        int addMoney = GetHasGrade() * Tools.perMoney;
        //更新金币
        SqliteHelper.GetInstance().UpdateOneData("MagicSquare", "money", addMoney + GetHasMoney(), GetUserName());
    }

    /// <summary>
    /// 获取人物拥有金币
    /// </summary>
    /// <returns>返回人物金币</returns>
    public int GetHasMoney()
    {
        //获取金币
        object obj = SqliteHelper.GetInstance().SelectRow("money", "MagicSquare", "userName", GetUserName());
        //string[] data = SqliteHelper.GetInstance().ReadData(SqliteHelper.GetInstance().Select(cmd));
        string data = obj + "";
        int userMoney = int.Parse(data);
        return userMoney;
    }

    #region 道具更新获取
    ///// <summary>
    ///// 任务拥有的道具
    ///// </summary>
    ///// <returns>返回道具</returns>
    //public string HasProp()
    //{
    //    //更新道具
    //    SqliteHelper.GetInstance().IDU("insert into Props values ('" + username + "'," + passRound + "," + grade + "," + rank + ",'" + prop + "'," + money + ")");

    //    //获取道具
    //    object obj = SqliteHelper.GetInstance().SelectRow("prop", "MagicSquare", "userName", userName);
    //    string data = obj + "";
    //    string userProp = data;
    //    return userProp;
    //}
    #endregion

    /// <summary>
    /// 更新人物所剩步数成绩
    /// </summary>
    /// <param name="step">传入所剩步数进行更新</param>
    public void SetHasGrade(int step)
    {
        //更新步数
        SqliteHelper.GetInstance().UpdateOneData("MagicSquare", "grade", step, GetUserName());
    }

    /// <summary>
    /// 获取数据库人物步数新成绩
    /// </summary>
    /// <returns>返回步数成绩</returns>
    public int GetHasGrade()
    {
        //获取步数
        object obj = SqliteHelper.GetInstance().SelectRow("MagicSquare", "grade", "userName", GetUserName());
        string data = obj + "";
        int userGrade = int.Parse(data);
        return userGrade;
    }

    /// <summary>
    /// 设置人物是否过关,更新关数 + 1
    /// </summary>
    public void SetPassRound()
    {
        //更新是否闯关
        SqliteHelper.GetInstance().UpdateOneData("MagicSquare", "passRound", GetPassRound() + 1, GetUserName());
    }

    /// <summary>
    /// 获取人物关数,返回关数
    /// </summary>
    /// <returns></returns>
    public int GetPassRound()
    {

        //获取是否闯关
        object obj = SqliteHelper.GetInstance().SelectRow("MagicSquare", "passRound", "userName", GetUserName());
        string data = obj + "";
        int userPassRound = int.Parse(data);
        return userPassRound;
    }

}
